﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MyBet.Helpers;

namespace MyBet.Class
{
    public class EventHelper
    {
        /// <summary>
        /// Renvoie l'identifiant de l'événément pour lequel le pari est ouvert
        /// ou en attente des résultats
        /// </summary>
        /// <returns></returns>
        public static int GetCurrentEventId(Dictionary<int, EventServiceReference.EventObject > EventsDictionary)
        {
            EventServiceReference.EventObject currentEvent = (from entry in EventsDictionary
              where (entry.Value._openDate < DateTime.UtcNow)
              orderby entry.Value._openDate descending
              select entry.Value).FirstOrDefault();
            
            if (currentEvent == null)
            { 
                // First day ?
                EventServiceReference.EventObject firstEvent = (from entry in EventsDictionary
                                                                where DateTime.UtcNow < entry.Value._openDate
                                                                orderby entry.Value._openDate
                                                                select entry.Value).FirstOrDefault();
                if (firstEvent != null)
                {
                    return firstEvent._eventId;
                }

                // No more day to bet
                // Return last one
                EventServiceReference.EventObject lastEvent = (from entry in EventsDictionary
                            orderby entry.Value._closeDate descending
                            select entry.Value).FirstOrDefault();
                
                if (lastEvent==null)
                {
                    throw new Exception("No days in this championship"); 
                }
                return lastEvent._eventId;
            }
            return currentEvent._eventId;
        }

        /// <summary>
        /// Renvoie l'identifiant de l'événément pour lequel le pari est ouvert
        /// ou en attente des résultats
        /// </summary>
        /// <returns></returns>
        public static int? GetLastClosedEvent(Dictionary<int, EventServiceReference.EventObject> EventsDictionary)
        {
            EventServiceReference.EventObject currentEvent = (from entry in EventsDictionary
                                                              where (entry.Value._closeDate < DateTime.UtcNow)
                                                              orderby entry.Value._closeDate descending
                                                              select entry.Value).FirstOrDefault();

            if (currentEvent == null)
            {
                return null;
            }
            return currentEvent._eventId;
        }

        /// <summary>
        /// Renvoie l'événement du dictionnaire passé en paramètre à partir de son id
        /// </summary>
        /// <param name="EventsDictionary">Dictionnaire des événements</param>
        /// <param name="eventId">Identifiant de l'événement</param>
        /// <returns></returns>
        public static EventServiceReference.EventObject GetEventFromId(Dictionary<int, EventServiceReference.EventObject> EventsDictionary, int eventId)
        {
            EventServiceReference.EventObject returnEvent = (from entry in EventsDictionary
                                                              where (entry.Value._eventId == eventId)
                                                              select entry.Value).FirstOrDefault();
            if (returnEvent == null)
            {
                return null;
            }
            return returnEvent;
        }

        /// <summary>
        /// Renvoie le nom de l'événement
        /// </summary>
        /// <param name="currentEvent"></param>
        /// <returns></returns>
        public static String GetEventName(EventServiceReference.EventObject currentEvent)
        {
            return String.Format("{2} ({3}) / {0} {1}", LangHelper.GetString("DayLabel").Trim(), currentEvent._dayNumber, App.ChampionshipsCache[currentEvent._championshipId].Name.Trim(), currentEvent._seasonName.Trim());
        }

    }
}
